package pacman.protocol;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.regex.Pattern;

import maze.Coordinate;
import maze.Maze;
import robots.Ghost;
import robots.OwnGhost;

public class OwnGhostProtocolProcessor extends GhostProtocolProcessor{

	public OwnGhostProtocolProcessor(OwnGhost ghost) {
		super(ghost);
	}
	
	private OwnGhost getOWnGhost(){
		return (OwnGhost) getGhost();
	}
	
	@Override
	public void processSHOWMAP(){
		ArrayList<String> discovers = getOWnGhost().protocolSender.getSendDISCOVERs();
		ArrayList<String> barcodes = getOWnGhost().protocolSender.getSendBARCODEs();
		String position = getOWnGhost().protocolSender.getLastPOSITIONsend();
		for(String discoverMsg: discovers){
			if(discoverMsg != null){
				String newMsg = convertToRE(discoverMsg);
				getOWnGhost().protocolSender.sendPreProcessedMessage(newMsg);
			}
		}
		for(String barcodeMsg: barcodes){
			if(barcodeMsg != null){
				String newMsg = convertToRE(barcodeMsg);
				getOWnGhost().protocolSender.sendPreProcessedMessage(newMsg);
			}
		}
		if(position != null){
			getOWnGhost().protocolSender.sendPOSITION();
		}
	}
	
	/**
	 * Processes the PING command
	 * @throws IOException 
	 */
	@Override
	public void processPING(String sender, String message){
		getOWnGhost().protocolSender.sendPONG(sender, message);
	}

	/**
	 * Processes the PONG command
	 */
	@Override
	public void processPONG(String sender, String message){
		if(getGame().getGhostWithName(sender) == null){
			System.err.println("PONG gekregen van iemand die niet in de game zit, namelijk " + sender);
		}
		getOWnGhost().getWaitingForPONG().remove(getGame().getGhostWithName(sender));
	}
	public void processPACMAN(String name,int x, int y){
		Map<String,Maze> otherMazes= getGhost().getMazesOtherRobots(); 
		for(Ghost ghost: getGhost().getMatchedGhost()){
			if(ghost.getName().equals(name)){
				//System.out.println("NUUUUUUUU");
				Maze otherMaze=getGhost().getMazeOnName(name);
				Coordinate coord= new Coordinate(x,y);
				Coordinate pacmanCoord= otherMaze.reverseTransform(coord);
				//System.out.println(pacmanCoord);
				getGhost().setCoordinatePacman(pacmanCoord);
			}
		}
			
	}
	
	public static String convertToRE(String message){
		String [] parts = partitionStringCommand(message);
		String command = parts[1];
		command = "RE" + command;
		parts[1] = command;
		String newMessage = "";
		for(String part: parts){
			newMessage+= part + " ";
		}
		//Nu nog een " " teveel op einde
		newMessage = (String) newMessage.substring(0, newMessage.length() - 1);
		newMessage += "\n";
		return newMessage;
		
	}
	
	private static String[] partitionStringCommand(String input) {
		input = input.replace("\n", ""); // \n op einde verwijderen
		String[] parts = Pattern.compile(" +").split(input);
		return parts;
	}
	
	public static void main(String [] args){
		String message = "rood_cont_782 BARCODEAT 6,-4 20 3";
		System.out.println("message:");
		System.out.println(message);
		String processedMsg = convertToRE(message);
		System.out.println("Processed message:");
		System.out.println(processedMsg);
		System.out.println(processedMsg.equals("rood_cont_782 REBARCODEAT 6,-4 20 3\n"));
	}
}
